package com.fuelwallet.model;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class CurrentAccount {
	private SQLiteDatabase db;
	private currentQLiteHelper dbHelper;
	
	public CurrentAccount(Context context){
		dbHelper = new currentQLiteHelper(context);
	}
	
	//open a writable database
	public void open() throws SQLException {
		db = dbHelper.getWritableDatabase();
	}

	//close the database
	public void close() {
		dbHelper.close();
	}
	
	//if name has existed in database, return false 
	/*public boolean putCarProfile(CarProfile in_carprofile){
		boolean nameAvailable=true;
		List<String> nameList = this.getAccountNameList();
		
		if(nameList.indexOf(in_carprofile.getAccountName())!=-1){
			nameAvailable = false;
		}else{	
			db.execSQL("INSERT INTO " + currentQLiteHelper.TABLE_CURRENT + " (" 
				+ currentQLiteHelper.COLUMN_ACCOUNT + "," 
				+ currentQLiteHelper.COLUMN_DISPLACEMENT + "," 
				+ currentQLiteHelper.COLUMN_FUELNAME + ")"
				+ " VALUES (\""
				+ in_carprofile.getAccountName()+ "\"," 
				+ in_carprofile.getDisplacement()+ ","
				+ "\""+in_carprofile.getFuelName() +"\")" ); 
		}
		return nameAvailable;
	}*/
	public void setCurrentAccount(CarProfile in_carprofile){
		this.deleteCurrent();
		db.execSQL("INSERT INTO " + currentQLiteHelper.TABLE_CURRENT + " (" 
				+ currentQLiteHelper.COLUMN_ACCOUNT + "," 
				+ currentQLiteHelper.COLUMN_DISPLACEMENT + "," 
				+ currentQLiteHelper.COLUMN_FUELNAME + ")"
				+ " VALUES (\""
				+ in_carprofile.getAccountName()+ "\"," 
				+ in_carprofile.getDisplacement()+ ","
				+ "\""+in_carprofile.getFuelName() +"\")" );
	}
	
	/*public void deleteCarProfile(final String account_name){
		db.execSQL("DELETE FROM " + currentQLiteHelper.TABLE_CURRENT
				+ " WHERE " + currentQLiteHelper.COLUMN_ACCOUNT + " = " + "\""+account_name+"\"");
	}*/
	public void deleteCurrent(){
		//db.execSQL("TRUNCATE TABLE " + currentQLiteHelper.TABLE_CURRENT);
		db.execSQL("DELETE FROM " + currentQLiteHelper.TABLE_CURRENT);
		db.execSQL("VACUUM");
	}
	
	/*public CarProfile getCarProfile(final String account_name){
		CarProfile out_carprofile;
		float displacement;
		String fuel_name;
		String args[] = {account_name};
		
		Cursor result = db.rawQuery("SELECT " + currentQLiteHelper.COLUMN_DISPLACEMENT + "," 
				+ currentQLiteHelper.COLUMN_FUELNAME
				+ " FROM "+currentQLiteHelper.TABLE_CURRENT 
				+ " WHERE " + currentQLiteHelper.COLUMN_ACCOUNT + " = ?", args);
		
		result.moveToFirst();
		displacement = result.getFloat(0);
		fuel_name = result.getString(1);
		
		out_carprofile=new CarProfile(account_name, displacement, fuel_name);	
		
		result.close();
		return out_carprofile;
	}*/
	public CarProfile getCurrentAccount(){
		CarProfile out_carprofile;
		String account_name;
		float displacement;
		String fuel_name;
		String args[] = {};
		
		Cursor result = db.rawQuery("SELECT *" 
				+ " FROM "+currentQLiteHelper.TABLE_CURRENT, args);
		
		result.moveToFirst();
		account_name = result.getString(1);
		displacement = result.getFloat(2);
		fuel_name = result.getString(3);
		
		out_carprofile=new CarProfile(account_name, displacement, fuel_name);	
		
		result.close();
		return out_carprofile;
	}
	
	/*
	public List<String> getAccountNameList(){
		List<String> nameList = new ArrayList<String>();
		String[] args = { };
		
		Cursor result = db.rawQuery("SELECT " + currentQLiteHelper.COLUMN_ACCOUNT 
				+ " FROM " + currentQLiteHelper.TABLE_CURRENT, args);
		
		result.moveToFirst();
		while (!result.isAfterLast()) {
			nameList.add(result.getString(0));
			result.moveToNext();
		}
		
		result.close();
		return nameList;
	}
	*/
	public class currentQLiteHelper extends SQLiteOpenHelper {

		public static final String TABLE_CURRENT = "current";
		public static final String COLUMN_ID = "_id";
		public static final String COLUMN_ACCOUNT = "account_name";
		public static final String COLUMN_DISPLACEMENT = "displacement";
		public static final String COLUMN_FUELNAME = "fuel_name";

		private static final String DATABASE_NAME = "current.db";
		private static final int DATABASE_VERSION = 1;

		// Database creation SQL statement
		private static final String DATABASE_CREATE = "create table "
				+ TABLE_CURRENT + "( " 
				+ COLUMN_ID + " integer primary key autoincrement, " 
				+ COLUMN_ACCOUNT + " text not null, "
				+ COLUMN_DISPLACEMENT + " real not null, "
				+ COLUMN_FUELNAME + " text not null);";

		public currentQLiteHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}

		@Override
		public void onCreate(SQLiteDatabase database) {
			database.execSQL(DATABASE_CREATE);
		}

		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(currentQLiteHelper.class.getName(),
					"Upgrading database from version " + oldVersion + " to "
							+ newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS" + TABLE_CURRENT);
			onCreate(db);
		}
	}

}